module.exports = {
    initAudio: initAudio,
    audioPlay: audioPlay,
    getStoreAudio: getStoreAudio,
    checkStoreAudio: checkStoreAudio,
    setAudioOpen: setAudioOpen,

}
var app = getApp()
var plugin = requirePlugin("WechatSI")


function connSuccAudio() {
    plugin.textToSpeech({
        lang: "zh_CN",
        tts: true,
        content: '连接成功',
        success: function (res) {
            console.log("====>>连接成功 succ tts", res.filename)
            app.globalData.audio.connsucc = res.filename

            wx.setStorageSync('audio_connsucc', res.filename)
        },
        fail: function (res) {
            console.log("====>>连接成功 fail tts", res)
        }
    })
}

function connFailAudio() {
    plugin.textToSpeech({
        lang: "zh_CN",
        tts: true,
        content: '连接断开',
        success: function (res) {
            console.log("====>>连接断开 succ tts", res.filename)
            app.globalData.audio.connfail = res.filename
            wx.setStorageSync('audio_connfail', res.filename)

        },
        fail: function (res) {
            console.log("====>>连接断开 fail tts", res)
        }
    })
}

function speedlt() {
    plugin.textToSpeech({
        lang: "zh_CN",
        tts: true,
        content: '请加快速度',
        success: function (res) {
            console.log("====>>speedlt succ tts", res.filename)
            app.globalData.audio.speedlt = res.filename
            wx.setStorageSync('audio_speedlt', res.filename)

        },
        fail: function (res) {
            console.log("====>>speedlt fail tts", res)
        }
    })
}

function speedeq() {
    plugin.textToSpeech({
        lang: "zh_CN",
        tts: true,
        content: '请保持速度',
        success: function (res) {
            console.log("====>>speedeq succ tts", res.filename)
            app.globalData.audio.speedeq = res.filename
            wx.setStorageSync('audio_speedeq', res.filename)

        },
        fail: function (res) {
            console.log("====>>speedeq fail tts", res)
        }
    })
}

function speedgt() {
    plugin.textToSpeech({
        lang: "zh_CN",
        tts: true,
        content: '请降低速度，注意安全',
        success: function (res) {
            console.log("====>>speedgt succ tts", res.filename)
            app.globalData.audio.speedgt = res.filename
            wx.setStorageSync('audio_speedgt', res.filename)

        },
        fail: function (res) {
            console.log("====>>speedgt fail tts", res)
        }
    })
}

function initAudio() {
    console.log('====>>初始化语音');
    //创建内部 audio 上下文 InnerAudioContext 对象。
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.onError(function (res) {
        console.log('====>>语音播放失败');
        console.log(res);
        // wx.showToast({
        //     title: '语音播放失败',
        //     icon: 'none',
        // })
        connSuccAudio()
        connFailAudio()
        speedlt()
        speedeq()
        speedgt()
    })

}

function audioPlay(src) {
    console.log('====>>播放语音');
    if (src == '') {
        console.log('暂无语音');
        checkStoreAudio()
        return;
    }
    this.innerAudioContext.src = src
    this.innerAudioContext.play();
}

function getStoreAudio() {
    app.globalData.audio.connsucc = wx.getStorageSync('audio_connsucc')
    app.globalData.audio.connfail = wx.getStorageSync('audio_connfail')
    app.globalData.audio.speedlt = wx.getStorageSync('audio_speedlt')
    app.globalData.audio.speedeq = wx.getStorageSync('audio_speedeq')
    app.globalData.audio.speedgt = wx.getStorageSync('audio_speedgt')
    app.globalData.audio.open = wx.getStorageSync('audio_open')
}

function checkStoreAudio() {
    if (app.globalData.audio.connsucc == '') {
        connSuccAudio()
    }
    if (app.globalData.audio.connfail == '') {
        connFailAudio()
    }
    if (app.globalData.audio.speedlt == '') {
        speedlt()
    }
    if (app.globalData.audio.speedeq == '') {
        speedeq()
    }
    if (app.globalData.audio.speedgt == '') {
        speedgt()
    }

}

function setAudioOpen(b) {

    app.globalData.audio.open = b

    wx.setStorageSync('audio_open', b)

}